<?php
namespace Client\Controller;
use Think\Page;

use Think\Controller;
use Org\Net\Http;
use Admin\Model\ScenicTicketModel;
class ScenicController extends Controller {
	public function query(){
		$http = new Http();
		$geotable_id ='111203';
		$city = '温州';
		$url = 'http://api.map.baidu.com/geosearch/v3/local'."?ak=".C('GSBAIDU_AK')."&geotable_id={$geotable_id}&page_size=10&region={$city}";
		$back =  $http->fsockopenDownload($url);
		$back = json_decode($back,'json');
		if($back['status']==0){
			$names = array();
			foreach($back['contents'] as $k=>$v){
				array_push($names,$v['title']);
			}
			if(sizeof($names)>0){
				$scenic = D("Scenic");
				$where['name']=array("in",$names);
				if($_POST['type']!=""){
					$where['type']=array("like",$_POST['type']);
				}
				$list = $scenic->where($where)->field("id,name,image,price,address")->select();
				if($list){
					$json['result'] = '0';
					$json['msg'] 	= '成功获取景区信息';
					$json['scenic'] = $list;
				}else{
					$json['result'] = '-1';
					$json['msg']    = '获取信息失败';
				}
			}
		}else {
					$json['result'] = '-2';
					$json['msg']    = '当前城市没有景点';
			}
		$this->ajaxReturn($json);
	}
	public function get(){
		$http = new Http();
		$geotable_id ='111203';
		$city = '温州';
		$url = 'http://api.map.baidu.com/geosearch/v3/local'."?ak=".C('GSBAIDU_AK')."&geotable_id={$geotable_id}&page_size=10&region={$city}";
		$back =  $http->fsockopenDownload($url);
		$back = json_decode($back,'json');
		if($back['status']==0){
			foreach($back['contents'] as $k=>$v){
				echo $v['title'].'<br/>';
			}
		}
	}
	public function getlocation(){
		$scenic = D("Scenic");
		$list = $scenic->field("id,name,latitude,longitude")->select();
		if($list){
			$json['result'] = '0';
			$json['scenic'] = $list;
		}else{
			$json['result'] = '-1';
		}
		$this->ajaxReturn($json);
	}
	public function getIndex(){
		if(IS_POST){
				$scenic = D("Scenic");
				$where['id'] = $_POST['id'];
				$list = $scenic->where($where)->find();
				if($list){
					$json['result'] = '0';
					$json['scenic'] = $list;
					$ticket = D("ScenicTicket");
					$where2['scenic_id'] = $_POST['id'];
					$list = $ticket->where($where2)->select();
					if($list){
						$json['tickets'] = $list;
					}
				}else{
					$json['result'] = '-1';
				}
				$this->ajaxReturn($json);
			}
	}
	public function getScenic(){
		if(IS_POST){
			$scenic = D("Scenic");
			$where['id'] = $_POST['id'];
			$list = $scenic->where($where)->find();
			if($list){
				$json['result'] = '0';
				$json['scenic'] = $list;
				$ticket = D("ScenicTicket");
				$where2['scenic_id'] = $_POST['id'];
				$where2['type']	     = 0;
				$list = $ticket->where($where2)->select();
				if($list){
					$json['tickets'] = $list;
					$json['msg'] = "成功获取";
				}
			}else{
				$json['result'] = '-1';
				$json['msg'] = "获取失败";
			}
			$this->ajaxReturn($json);
		}
	}
	public function getEntertainment(){
		if(IS_POST){
			    $page_num = I('page_num');
				$ticket = D("ScenicTicket");
				$where2['type']	     = 1;
				if(!$enresult = S('enresult')){
					$enresult = $ticket->relation(true)->where($where2)->select();
					S('enresult',$enresult,300);
				}
				if(sizeof($enresult)>0){
					$start = $page_num*10;
					$res = array_slice($enresult,$start,10);
					$ticketres = array();
					foreach ($res as $k=>$v){
						$ticketres[$k]['ticket_time'] = date('Y-m-d H:i:s',$v['time']);
						$ticketres[$k]['ticket_id']   = $v['id'];
						$ticketres[$k]['ticket_name'] = $v['name'];
						$ticketres[$k]['ticket_price'] = $v['price'];
						$ticketres[$k]['ticket_deadline'] = $v['deadline']; 
						$ticketres[$k]['ticket_original'] = $v['original'];
						$ticketres[$k]['ticket_image'] = $v['image'];
						$ticketres[$k]['scenic_image'] = $v['scenic_image'];
						$ticketres[$k]['scenic_id']=$v['scenic_id'];
						$ticketres[$k]['scenic_name']=$v['scenic_name'];
						$ticketres[$k]['scenic_address']=$v['scenic_address'];
						$ticketres[$k]['scenic_latitude']=$v['scenic_latitude'];
						$ticketres[$k]['scenic_longitude']=$v['scenic_longitude'];
						$ticketres[$k]['scenic_phone'] = $v['scenic_phone'];
					}
					$json['tickets'] = $ticketres;
					$json['result']  = '0';
					$json['msg'] = "成功获取";
				}
			}else{
				$json['result'] = '-1';
				$json['msg'] = "获取失败";
			}
			$this->ajaxReturn($json);
		}
}